<?php

class MemdaysController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_memdays where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select * from crm_memdays where deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $memdays_id = $_REQUEST['memdays_id'];

        $memdays = null;
        if($memdays_id==0){
            //新增
        }else{
            //编辑
            $memdays = $this->connection->createCommand("select * from crm_memdays where memdays_id = :memdays_id")->bindParam(':memdays_id',$memdays_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['memdays'=>$memdays]);
    }

    public function actionSavedata(){

        $memdays_id = $_REQUEST['memdays_id'];
        $title = $_REQUEST['title'];
        $customer = $_REQUEST['customer'];
        $memdays_type = $_REQUEST['memdays_type'];
        $calendar_type = $_REQUEST['calendar_type'];
        $memdays_day = $_REQUEST['memdays_day'];

        if($memdays_id!=null && $memdays_id!=""){
            //编辑
            $this->connection->createCommand("update crm_memdays set title = :title,customer = :customer,
memdays_type=:memdays_type,calendar_type=:calendar_type,memdays_day=:memdays_day where memdays_id = :memdays_id")
                ->bindParam(':memdays_id',$memdays_id,PDO::PARAM_INT)
                ->bindParam(':title',$title,PDO::PARAM_STR)
                ->bindParam(':customer',$customer,PDO::PARAM_STR)
                ->bindParam(':memdays_type',$memdays_type,PDO::PARAM_STR)
                ->bindParam(':calendar_type',$calendar_type,PDO::PARAM_STR)
                ->bindParam(':memdays_day',$memdays_day,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_memdays (title,customer,memdays_type,calendar_type,memdays_day) 
values (:title,:customer,:memdays_type,:calendar_type,:memdays_day) ")
                ->bindParam(':title',$title,PDO::PARAM_STR)
                ->bindParam(':customer',$customer,PDO::PARAM_STR)
                ->bindParam(':memdays_type',$memdays_type,PDO::PARAM_STR)
                ->bindParam(':calendar_type',$calendar_type,PDO::PARAM_STR)
                ->bindParam(':memdays_day',$memdays_day,PDO::PARAM_STR)
                ->execute();
        }
    }

    public function actionRemove(){
        $memdays_id = $_REQUEST['memdays_id'];
        $this->connection->createCommand("update crm_memdays set deleted = 1 
where memdays_id = :memdays_id")
            ->bindParam(':memdays_id',$memdays_id,PDO::PARAM_INT)
            ->execute();
    }

    public function actionView(){

        $memdays_id = $_REQUEST['memdays_id'];
        $memdays = $this->connection->createCommand("select * from crm_memdays where memdays_id = :memdays_id")->bindParam(':memdays_id',$memdays_id,PDO::PARAM_INT)->queryRow();

        $this->render('view',['memdays'=>$memdays]);
    }

}